"use strict";

app.controller("BottomSheetEditorCtrl", ["$scope", "$http", "$mdBottomSheet", "$mdDialog", "$filter", function($scope, $http, $mdBottomSheet, $mdDialog, $filter){
    $scope.dateClick = function () {
        function DialogController($scope, $mdDialog) {
            $scope.value = new Date();
            $scope.$watch('value', function (newValue, oldValue) {
                if (newValue === oldValue || !newValue) return;
                $scope.answer("ok")
            });
            $scope.hide = function () {
                $mdDialog.hide();
            };
            $scope.cancel = function () {
                $mdDialog.cancel();
            };
            $scope.answer = function (answer) {
                var value = $filter('date')($scope.value, "EEEE d MMMM yyyy");
                $mdDialog.hide(value);
            };
        };
        var modalInstance = $mdDialog.show({
            templateUrl: '/pro/partials/editors/datepicker.html',
            controller: DialogController
        }).then(function (result) {
            $mdBottomSheet.hide(result);
        }, function () {
            $mdBottomSheet.cancel();
        });
    };

    $scope.timeClick = function () {
        function DialogController($scope, $mdDialog) {
            var dt = new Date();
            var offset = 0;/*(dt.getMinutes() % 15) - 15;*/
            $scope.value = new Date(dt.getTime() - offset * 60000);

            $scope.hstep = 1;
            $scope.mstep = 1;
            $scope.ismeridian = false;

            $scope.hide = function () {
                $mdDialog.hide();
            };

            $scope.cancel = function () {
                $mdDialog.cancel();
            };

            $scope.answer = function (answer) {
                var value = $filter('date')($scope.value, "H'h' mm");
                $mdDialog.hide(value);
            };
        };
        var modalInstance = $mdDialog.show({
            templateUrl: '/pro/partials/editors/timepicker.html',
            controller: DialogController
        }).then(function (result) {
            $mdBottomSheet.hide(result);
        }, function () {
            $mdBottomSheet.cancel();
        });
    };

    $scope.priceClick = function () {
        function DialogController($scope, $mdDialog) {
            $scope.value = null;
            $scope.increment = function () {
                $scope.value += 1;
            };
            $scope.decrement = function () {
                $scope.value -= 1;
            };

            $scope.hide = function () {
                $mdDialog.hide();
            };

            $scope.cancel = function () {
                $mdDialog.cancel();
            };

            $scope.answer = function (answer) {
                var value = $filter('currency')($scope.value, "€");
                $mdDialog.hide(value);
            };
        };

        $scope.value = new Date();
        var modalInstance = $mdDialog.show({
            templateUrl: '/pro/partials/editors/price.html',
            controller: DialogController
        }).then(function (result) {
            $mdBottomSheet.hide(result);
        }, function () {
            $mdBottomSheet.cancel();
        });
    };

    $scope.percentClick = function () {
        function DialogController($scope, $mdDialog) {
            $scope.value = null;
            $scope.increment = function () {
                $scope.value += 1;
            };
            $scope.decrement = function () {
                $scope.value -= 1;
            };

            $scope.hide = function () {
                $mdDialog.hide();
            };

            $scope.cancel = function () {
                $mdDialog.cancel();
            };

            $scope.answer = function (answer) {
                var value = $scope.value + "%";
                $mdDialog.hide(value);
            };
        };

        $scope.value = new Date();
        var modalInstance = $mdDialog.show({
            templateUrl: '/pro/partials/editors/percent.html',
            controller: DialogController
        }).then(function (result) {
            $mdBottomSheet.hide(result);
        }, function () {
            $mdBottomSheet.cancel();
        });
    };
    $scope.hashClick = function () {
        function DialogController($scope, $mdDialog) {
            $scope.hashtag = "";

            $scope.search = function (value) {
                return $http.get("/api/hashtag/search", { params: { q: value, skip: 0, limit: 10} }).then(function (res) {
                    return res.data;
                });
            };
 
            $scope.hide = function () {
                $mdDialog.hide();
            };

            $scope.cancel = function () {
                $mdDialog.cancel();
            };

            $scope.answer = function (answer) {
                var value = "#" + $scope.hashtag;
                $mdDialog.hide(value);
            };
        };

        var modalInstance = $mdDialog.show({
            templateUrl: '/pro/partials/editors/hashtag.html',
            controller: DialogController
        }).then(function (result) {
            $mdBottomSheet.hide(result);
        }, function () {
            $mdBottomSheet.cancel();
        });
    };
    $scope.linkClick = function () {
        function DialogController($scope, $mdDialog) {
            $scope.login = "";

            $scope.search = function (value) {
                return $http.get("api/references/search", { params: { q: value, skip: 0, limit: 20} }).then(function (res) {
                    return res.data;
                });
            };
                    
            $scope.hide = function () {
                $mdDialog.hide();
            };

            $scope.cancel = function () {
                $mdDialog.cancel();
            };

            $scope.answer = function (answer) {
                var value = "@" + $scope.login;
                $mdDialog.hide(value);
            };
        };

        $scope.value = new Date();
        var modalInstance = $mdDialog.show({
            templateUrl: '/pro/partials/editors/link.html',
            controller: DialogController
        }).then(function (result) {
            $mdBottomSheet.hide(result);
        }, function () {
            $mdBottomSheet.cancel();
        });
    };
} ]);
